Maya=function(){
	this.version = "1.3";
	this.author = "Maya";
}
Maya.Box=function(obj){
	var this_ref=this,prefix="MayaBox_",o=obj;
	//设置参数
	this.setOption=function(){
		try{
			this.client={ 
				clientX : event.clientX,
				clientY : event.clientY
			}
		}catch(e){
		};
		//标题
		this.text=o.text==undefined ? "MayaBox" : o.text;
		//宽度
		if(o.width==undefined){
			this.globalWidth=this.width="auto";
		}else if(String(o.width).indexOf("%")>0){
			this.globalWidth=this.width=parseInt(document.documentElement.clientWidth*parseInt(o.width)/100)+"px";
		}else{
			this.globalWidth=this.width=parseInt(o.width)+4+"px";
		}
		//高度
		if(o.height==undefined){
			this.globalHeight=this.height="auto";
		}else if(String(o.height).indexOf("%")>0){
			this.globalHeight=this.height=parseInt(document.documentElement.clientHeight*parseInt(o.height)/100)-32+"px";
		}else{
			this.globalHeight=this.height=o.height+"px";
		}
		//显示状态
		this.status="hide";
		//样式
		this.theme=o.theme==undefined ? "blue" : o.theme;
		//渲染类型
		this.type=o.type==undefined ? "content" : o.type; // iframe,ajax,content
		//唯一ID
		this.id=o.id==null ? Math.ceil(Math.random()*999999) : o.id;
		//背景ID
		this.bgId="MayaBox_Bg_"+this.id;
		//ajax的url
		this.url=o.url==undefined ? "#" : o.url;
		//url参数，JSON对象
		this.url_param=o.url_param==undefined ? {} : o.url_param;
		//是否创建完自动打开
		this.autoOpen=o.autoOpen==undefined ? true : o.autoOpen;
		//是否显示下面的三角
		this.showSub=o.showSub==undefined ? false : o.showSub;
		//是否显示背景
		this.showBg=o.showBg==undefined ? true : o.showBg;
		//是否在点击的地方显示
		this.isPoint=o.isPoint==undefined ? false : o.isPoint;
		//要显示的内容
		this.content=o.content==undefined ? "" : o.content;
		//关闭方式
		this.closeType=o.closeType==undefined ? "clear" : o.closeType;
		//标签元素
		this.ele_bg=null;	this.ele_panel=null;	this.ele_header=null;	this.ele_content=null;	this.ele_sub=null;	this.ele_header_text=null;	this.ele_header_close_btn=null;
	}
	//关闭窗口
	this.close=function(){
		this.status="hide";
		//this.bgHide();
		$("#"+prefix+this.id).fadeOut(300,function(){
			if(this_ref.closeType=="clear"){
				//移除iframe，解决IE文本框无法获得焦点的问题
				if(this_ref.type=="iframe"){
					this_ref.ele_content.innerHTML="";
				}
				document.body.removeChild(this_ref.ele_bg);
				this_ref=null;
				$(window).unbind("resize");
				$(window).unbind("scroll");
				//document.body.removeChild(this_ref.ele_bg);
			}else{
				$("#"+this_ref.bgId).fadeOut(400);
			}
		});
	}
	//恢复窗口
	this.resize=function(){
		if(!this.isPoint){
			var st=document.body.scrollTop || document.documentElement.scrollTop;
			var sl=document.body.scrollLeft || document.documentElement.scrollLeft;
			var css_left=parseInt(document.documentElement.clientWidth/2)+sl-Math.ceil(this.globalWidth/2)+"px";
			var css_top=parseInt(document.documentElement.clientHeight/2)+st-Math.ceil(this.globalHeight/2)+"px";
			$("#"+prefix+this.id).animate({
				left : css_left,
				top : css_top
			});
		}
	}
	//打开窗口
	this.open=function(){
		//将整体的宽高记录
		this.ele_panel.style.display="block";
		this.globalWidth=this.ele_panel.offsetWidth;
		this.globalHeight=this.ele_panel.offsetHeight;
		this.ele_panel.style.display="none";
		this.status="show";
		var st=document.body.scrollTop || document.documentElement.scrollTop;
		var sl=document.body.scrollLeft || document.documentElement.scrollLeft;
		//居中
		if(!this.isPoint){
			var css_left=parseInt(document.documentElement.clientWidth/2)+sl-Math.ceil(this.globalWidth/2)+"px";
			var css_top=parseInt(document.documentElement.clientHeight/2)+st-Math.ceil(this.globalHeight/2)+"px";
		}else{
		//在点击的地方居中
			//如果是火狐就直接居中显示
			if($.browser.mozilla){
				this.isPoint=false;
				this.ele_panel.style.display="block";
				this.open();
				return;
			}else{
				var css_left=this_ref.client.clientX-Math.ceil(this.globalWidth/2)+sl+"px";
				var css_top=this_ref.client.clientY-this.globalHeight-16+st+"px";
			}
		}
		//alert(css_left+"---"+css_top+"---"+css_margin_top);
/*		$("#"+prefix+this.id).animate({
			left : css_left,
			top : css_top
		});*/
		$("#"+prefix+this.id).css({left : css_left,	top : css_top});
		$("#"+prefix+this.id).fadeIn(400);
	}
	//隐藏背景
	this.bgHide=function(){
		if(this.showBg)$("#"+this.bgId).fadeOut();
	}
	//显示背景
	this.bgShow=function(){
		if(this.showBg)$("#"+this.bgId).fadeIn();
	}
	this.createPanel=function(){
		//创建布局
		this.ele_panel=document.createElement("div");
		this.ele_panel.className="MayaBox MayaBox_Theme_"+this.theme;
		this.ele_panel.setAttribute("id",prefix+this.id);
		this.ele_panel.style.display="none";
		this.ele_bg.appendChild(this.ele_panel);

	}
	this.createHeader=function(){
		//创建头标签
		this.ele_header=document.createElement("div");
		this.ele_header.className="MayaBox_Header";
		this.ele_header.style.width = this.width=="auto" ? this.width : parseInt(this.width)+4+"px";
		//创建头文本标签
		this.ele_header_text=document.createElement("span");
		this.ele_header_text.className="MayaBox_Header_Text";
		this.ele_header_text.innerHTML=this.text;
		//创建头关闭按钮标签
		this.ele_header_close_btn=document.createElement("a");
		this.ele_header_close_btn.className="MayaBox_Header_CloseBtn";
		//添加头关闭按钮事件
		this.ele_header_close_btn.onclick=function(){
			this_ref.close();
		}
		//附加到header
		this.ele_header.appendChild(this.ele_header_text);
		this.ele_header.appendChild(this.ele_header_close_btn);
		//附加到panel
		this.ele_panel.appendChild(this.ele_header);
	}
	this.createSub=function(){
		//创建底部箭头
		this.ele_sub=document.createElement("div");
		this.ele_sub.className="MayaBox_Sub";
		//附加到panel
		this.ele_panel.appendChild(this.ele_sub);
	}
	this.createContent=function(){
		//创建内容标签
		this.ele_content=document.createElement("div");
		this.ele_content.setAttribute("id","MayaBox_Content_"+this.id);
		this.ele_content.className="MayaBox_Content";
		this.ele_content.style.width = this.width;
		this.ele_content.style.height = this.height;
		
		//附加到panel
		this.ele_panel.appendChild(this.ele_content);
	}
	this.setContent=function(content){
		this.ele_content.innerHTML=content;
		//如果自动打开窗口
		if(this.autoOpen){this.open();}
	}
	this.reload=function(){
		this.setOption();
		this.loadContent();	
	}
	this.loadContent=function(fn){
		switch(this.type){
			case "iframe" :
				div_content='<iframe id="iframe_'+this.id+'" src="'+this.url+'" width="'+parseInt(this.width)+'" height="'+parseInt(this.height)+'" allowtransparency="true" frameborder="0" ></iframe>';
				this.setContent(div_content);
				break;
			case "ajax" :
				$.get(
					this.url,
					this.url_param,
					function(data){
						this_ref.setContent(data);
					},
					"html"
				);
				break;
			case "content" :
			default:
				this.setContent(this.content);
		}
	}
	//创建窗口
	this.create=function(){
		//初始化参数
		this.setOption();
		//创建背景标签
		this.ele_bg=document.createElement("div");
		this.ele_bg.className="MayaBox_Bg_None";
		this.ele_bg.style.height=(document.body.scrollHeight || document.documentElement.scrollHeight)+"px";
		this.ele_bg.setAttribute("id",this.bgId);
		document.body.appendChild(this.ele_bg);
		//创建布局
		this.createPanel();
		//创建标题栏
		this.createHeader();
		//创建内容区域
		this.createContent();
		//创建底部箭头
		if(this.showSub)this.createSub();
		//载入要显示的内容
		this.loadContent();
		//如果需要显示背景，那么就创建背景
		if(this.showBg)this.ele_bg.className="MayaBox_Bg";
		//添加滚动条事件
		$(window).bind("resize",function(){this_ref.resize();});
		$(window).bind("scroll",function(){this_ref.resize();});
		//$(window).scroll(function(){if(this_ref.status=="show")this_ref.open();});
	}
	//调用创建窗口
	this.create();
}
